//
//  HYFEmoticonPageViewCell.swift
//  MyWeibo
//
//  Created by 黄勇锋 on 16/7/29.
//  Copyright © 2016年 黄勇锋. All rights reserved.
//

import UIKit

class HYFEmoticonPageViewCell: UICollectionViewCell {
    
    var buttons:[HYFEmoticonButton] = [HYFEmoticonButton]()
    
    //定义一个装有模型的数组
    var emoticons:[HYFEmoticon]?{
        
        didSet{
            
           //隐藏所有的按钮
            for value in buttons{
                
              value.hidden = true
            }
            //遍历表情数组
            for (i ,emoticon) in emoticons!.enumerate(){
                
                //得到对应的button
                let button = buttons[i]
                
                //赋值
                button.emoticon = emoticon
                //显示按钮
                button.hidden = false
                //如果是 emoji 表情
                if emoticon.isEmoji {
                    
                    button.setTitle((emoticon.code ?? "" as NSString).emoji(), forState: UIControlState.Normal)
                    
                    //解决 image 和 title 重复问题
                    button.setImage(nil, forState: .Normal)
                }else{
                    
                    //代表图片表情
                    let image = UIImage.getEmoticonImage(emoticon.path)
                    button.setImage(image, forState: .Normal)
                    
                    //解决 title 和 image 重复的问题
                    button.setTitle(nil, forState: .Normal)
                }
            }
        }
    }
    
    var indexPath:NSIndexPath?{
        
        didSet{
           
            messageLabel.text = "第\(indexPath?.section ?? 0)组\n第\( indexPath?.row ?? 0)页"
        }
    }


    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setupUI(){
        
        backgroundColor = RandomColor()
        
        addchildButton()
        addSubview(deleteButton)
        
//        //添加控件
//        contentView.addSubview(messageLabel)
//        //设置约束
//        messageLabel.snp_makeConstraints { (make) in
//            
//            make.center.equalTo(contentView)
//        }
    }
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        let childW = (self.frame.width - 10) / CGFloat(HYFEmoticonMaxCol)
        let childH = (self.frame.height - 20) / CGFloat(HYFEmoticonMaxRow)
        
        for (i,value) in buttons.enumerate(){
            
          let colIndex = CGFloat(i%HYFEmoticonMaxCol)
          let rowIndex = CGFloat(i/HYFEmoticonMaxCol)
            
            value.frame = CGRect(x: childW * colIndex + 5, y: childH * rowIndex, width: childW, height: childH)
        }
        
        //设置删除按钮的 frame
        deleteButton.frame = CGRect(x: self.frame.width - childW - 5, y: childH * 2, width: childW, height: childH)
    }
    
    //监听方法的实现
    func buttonClick(button:HYFEmoticonButton){
        
       NSNotificationCenter.defaultCenter().postNotificationName(WBemoticonButtonNoti, object: button.emoticon)
        
    }
    
    //添加按钮
    func addchildButton(){
        
        for _ in 0..<HYFEmoticonMaxCount{
            
            let button = HYFEmoticonButton()
            
            //添加点击事件
            button.addTarget(self, action: "buttonClick:", forControlEvents: .TouchUpInside)
            
            button.backgroundColor = RandomColor()
            contentView.addSubview(button)
            button.titleLabel?.font = UIFont.systemFontOfSize(30)
            
            
            
           buttons.append(button)
        }
    }
    
    //添加监听
    func deleteButtonClick(){
        
        //发送通知
        NSNotificationCenter.defaultCenter().postNotificationName(WBEmoticonDeleteButtonNoti, object: nil)
        
    }
    
    private lazy var messageLabel = UILabel(textColor: UIColor.redColor(), textFont: 35, maxWidth: SCREENW - 40)
  
    
    //懒加载删除按钮
    private lazy var deleteButton:UIButton = {
        
       let button = UIButton()
        button.setImage(UIImage(named:"compose_emotion_delete"), forState: .Normal)
        button.setImage(UIImage(named:"compose_emotion_delete_highlighted"), forState: .Highlighted)
        
        //添加点击事件
        button.addTarget(self, action: "deleteButtonClick", forControlEvents: .TouchUpInside)
        
        return button
    }()
    
}
